/**
 * @project walk-exchange-market  V1.0
 * @filename: KLineUtil.java 2020年8月6日
 * Copyright(c) 2020 kinbug Co. Ltd. 
 * All right reserved. 
 */
package com.gitee.kinbug.sniper.common.util;

import java.util.Calendar;

import com.gitee.kinbug.sniper.common.enums.CandleEnum;

import lombok.extern.slf4j.Slf4j;

/**
 * @author kinbug
 * @ClassName: KLineUtil
 * @Description: K线工具类
 * @date 2020年8月6日
 */
@Slf4j
public class KlineUtil {

	/**
	 * 获取当前K线
	 *
	 * @param kLineTypeEnum kLineTypeEnum
	 * @param current       current 时间戳
	 * @return Long 20200909232300
	 */
	public static Long getKlineId(CandleEnum ciEnum, long current) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(current);
		switch (ciEnum) {
			case MIN1:
				int mins = cal.get(Calendar.MINUTE);
				cal.set(Calendar.MINUTE, mins);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case MIN5:
				int mins5 = cal.get(Calendar.MINUTE);
				int excess5 = mins5 % 5;
				cal.set(Calendar.MINUTE, mins5 - excess5);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case MIN15:
				int mins15 = cal.get(Calendar.MINUTE);
				int excess15 = mins15 % 15;
				cal.set(Calendar.MINUTE, mins15 - excess15);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case MIN30:
				int mins30 = cal.get(Calendar.MINUTE);
				int excess30 = mins30 % 30;
				cal.set(Calendar.MINUTE, mins30 - excess30);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case HOUR1:
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case HOUR4:
				int hour4 = cal.get(Calendar.HOUR_OF_DAY);
				int excessH4 = hour4 % 4;
				cal.set(Calendar.HOUR_OF_DAY, hour4 - excessH4);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case DAY1:
				cal.set(Calendar.HOUR_OF_DAY, 0);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case WEEK1:
				cal.setFirstDayOfWeek(Calendar.SUNDAY);
				cal.set(Calendar.HOUR_OF_DAY, 0);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
				return cal.getTime().getTime();
			case MON1:
				cal.set(Calendar.HOUR_OF_DAY, 0);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				cal.set(Calendar.DAY_OF_MONTH, 1);
				return cal.getTime().getTime();
			default:
				break;
		}
		log.error("getKlineId-获取KlineId异常,{},{}", ciEnum, current);
		throw new RuntimeException("getKlineId-获取KlineId异常");
	}

	/**
	 * 前多少根K线id
	 *
	 * @param kLineTypeEnum kLineTypeEnum
	 * @param current       current 20200909232300
	 * @return Long 20200909232300
	 */
	public static Long getAgoKlineId(CandleEnum ciEnum, long current , int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(current);
		switch (ciEnum) {
			case MIN1:
				int mins = cal.get(Calendar.MINUTE);
				cal.set(Calendar.MINUTE, mins - num);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case MIN5:
				int mins5 = cal.get(Calendar.MINUTE);
				int excess5 = mins5 % 5;
				cal.set(Calendar.MINUTE, mins5 - excess5 - 5*num);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case MIN15:
				int mins15 = cal.get(Calendar.MINUTE);
				int excess15 = mins15 % 15;
				cal.set(Calendar.MINUTE, mins15 - excess15 - 15*num);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case MIN30:
				int mins30 = cal.get(Calendar.MINUTE);
				int excess30 = mins30 % 30;
				cal.set(Calendar.MINUTE, mins30 - excess30 - 30*num);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case HOUR1:
				int h = cal.get(Calendar.HOUR);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				cal.set(Calendar.HOUR, h - 1*num);
				return cal.getTime().getTime();
			case HOUR4:
				int hour4 = cal.get(Calendar.HOUR_OF_DAY);
				int excessH4 = hour4 % 4;
				cal.set(Calendar.HOUR_OF_DAY, hour4 - excessH4 - 4*num);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case DAY1:
				cal.set(Calendar.HOUR_OF_DAY, -24*num);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				return cal.getTime().getTime();
			case WEEK1:
				cal.setFirstDayOfWeek(Calendar.SUNDAY);
				cal.set(Calendar.HOUR_OF_DAY, 0);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
				cal.add(Calendar.DAY_OF_WEEK, -7*num);
				return cal.getTime().getTime();
			case MON1:
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				cal.add(Calendar.MONTH, -1);
				cal.set(Calendar.DAY_OF_MONTH, 1*num);
				return cal.getTime().getTime();
			default:
				break;
		}
		log.error("getLastKlineId-获取KlineId异常,{},{}", ciEnum, current);
		throw new RuntimeException("getLastKlineId-获取KlineId异常");
	}
}
